package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.PerfBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.PerfDao;
import com.jackiechancinema.ssm.dto.PerfExcelDto;
import com.jackiechancinema.ssm.entity.Perf;
import com.jackiechancinema.ssm.listener.PerfReadListener;
import com.jackiechancinema.ssm.service.Inte.PerfService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class PerfServiceImpl implements PerfService {
    @Autowired
    private PerfDao perfDao;
    @Autowired
    private PerfReadListener perfReadListener;
    @Override
    public PageVo<Perf> queryAll(PerfBo pageBo) {
        //dao层获取到的只是Data里面的List集合
        PageHelper.startPage(pageBo.getPage(),pageBo.getLimit());
        PageInfo<Perf>pageInfo = new PageInfo<Perf>(perfDao.queryAll(pageBo));
        return new PageVo<Perf>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R insert(Perf perf) {
        System.out.println(1);
        System.out.println(perf);
        int save = perfDao.insert(perf);
        R r = new R();
        if(save>0){
            r.setCode(SystemContant.R_OK);
            r.setMsg("添加成功");
        }else{
            r.setCode(SystemContant.R_FAIL);
            r.setMsg("添加失败");
        }
        return r;
    }

    @Override
    public R delete(int perf_id) {
        R r = new R();
        if(perfDao.delete(perf_id)>0){
            r.setCode(SystemContant.R_OK);
            r.setMsg("OK");
        }else{
            r.setCode(SystemContant.R_FAIL);
            r.setMsg("失败");
        }
        return r;
    }

    @Override
    public R update(Perf perf) {
        R r = new R();
        if(perfDao.update(perf)>0){
            r.setCode(SystemContant.R_OK);
            r.setMsg("OK");
        }else{
            r.setCode(SystemContant.R_FAIL);
            r.setMsg("fail");
        }
        return r;
    }

    @Override
    public R batchAdd(MultipartFile file) {
        try {
            if(!file.isEmpty()){
                EasyExcel.read(file.getInputStream(), PerfExcelDto.class,perfReadListener).sheet().doRead();
                return R.ok("添加成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.fail("添加失败");
    }
    //下载excel文件
    @Override
    public void exportExcel(PerfBo perfBo, HttpServletResponse response){
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);
        List<Perf> list = perfDao.queryAll(perfBo);
        try {
            //写入类，，，1.获取字节流  2.返回的类class文件     list：存放的对象
            EasyExcel.write(response.getOutputStream(),Perf.class).sheet().doWrite(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}